package top.kaoshanji.p1ch02b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Proxy;

/**
 * author kaoshanji
 * ^_^
 * create time 2024/12/23 20:23
 */
public class SimpleProxyV3 {

    private static final Logger logger = LogManager.getLogger(SimpleProxyV3.class);

    public static void main(String[] args) {

        String str = "Hello World";
        CharSequence cs = (CharSequence) proxyAll(str);

        cs.length();
        cs.charAt(0);
        cs.toString();

    }

    public static Object proxyAll(final Object object) {
        logger.info("...代码清单 p1ch02b-05：代理某个类所实现的所有接口(2.3.1节)...");

        LoggingInvocationHandler handler = new LoggingInvocationHandler(object);
        ClassLoader cl = object.getClass().getClassLoader();
        Class<?>[] interfaces = object.getClass().getInterfaces();
        return Proxy.newProxyInstance(cl, interfaces, handler);
    }




}
